/*************************************************************************
 *
 *
 *    (c) Copyright Olimex 2010
 *
 *    File name   : extcon_test.C
 *    Description : External connectors test
 *
 *    History :
 *    1. Date        : 25, January 2010
 *       Author      : Aleksandar Mitev
 *       Description : Create
 *
 **************************************************************************/
#include <stdio.h>

#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "PinManipulation.h"

#define GET_PIN(pin) (1 << (pin))


// make output
void MakePinInput(const ROM PIN* pPin)
{
	BYTE bitMask = GET_PIN(pPin->GPIO_Pin);
	
	switch(pPin->GPIO_Port) {
	case GPIOA:
		TRISA |= bitMask;
		break;
	case GPIOB:
		TRISB |= bitMask;
		break;
	case GPIOC:
		TRISC |= bitMask;
		break;
	}	
}

// make input
void MakePinOutput(const ROM PIN* pPin)
{
	BYTE bitMask = ~GET_PIN(pPin->GPIO_Pin);
	
	switch(pPin->GPIO_Port) {
	case GPIOA:
		TRISA &= bitMask;
		break;
	case GPIOB:
		TRISB &= bitMask;
		break;
	case GPIOC:
		TRISC &= bitMask;
		break;
	}	
}

// write 1 to output
void SetPin(const ROM PIN *pPin)
{
	BYTE bitMask = GET_PIN(pPin->GPIO_Pin);
	
	switch(pPin->GPIO_Port) {
	case GPIOA:
		LATA |= bitMask;
		break;
	case GPIOB:
		LATB |= bitMask;
		break;
	case GPIOC:
		LATC |= bitMask;
		break;
	}	
}

// write 0 to output
void ClearPin(const ROM PIN *pPin)
{
	BYTE bitMask = ~GET_PIN(pPin->GPIO_Pin);
	
	switch(pPin->GPIO_Port) {
	case GPIOA:
		LATA &= bitMask;
		break;
	case GPIOB:
		LATB &= bitMask;
		break;
	case GPIOC:
		LATC &= bitMask;
		break;
	}	
}

// read input pin
BYTE ReadPin(const ROM PIN *pPin)
{
	BYTE bitMask = GET_PIN(pPin->GPIO_Pin);
	BYTE result = 0;
	
	switch(pPin->GPIO_Port) {
	case GPIOA:
		result = PORTA & bitMask;
		break;
	case GPIOB:
		result = PORTB & bitMask;
		break;
	case GPIOC:
		result = PORTC & bitMask;
		break;
	}	
	
	return (result ? 1 : 0);
}


